package cloud.maque.module.system.service.impl;


import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.request.PageRequest;
import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.common.utils.PageRequestUtil;
import cloud.maque.module.system.entity.SysPrivilege;
import cloud.maque.module.system.mapper.PrivilegeMapper;
import cloud.maque.module.system.service.PrivilegeService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class PrivilegeServiceImpl extends ServiceImpl<PrivilegeMapper, SysPrivilege> implements PrivilegeService {


    @Autowired
    PageRequestUtil util;

    @Autowired
    TokenUserUtil userUtil;

    @Override
    public IPage<SysPrivilege> GetList(PageRequest<SysPrivilege> pageReq) {
        Page<SysPrivilege> page = util.GetPage(SysPrivilege.class, pageReq);
        QueryWrapper<SysPrivilege> query = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getPrivilegeName())) {
            query.lambda().like(SysPrivilege::getPrivilegeName, pageReq.getData().getPrivilegeName());
        }
        if (!StrUtil.isEmptyIfStr(pageReq.getData().getPrivilegeCode())) {
            query.lambda().like(SysPrivilege::getPrivilegeCode, pageReq.getData().getPrivilegeCode());
        }

        query.lambda().orderByAsc(SysPrivilege::getPrivilegeSort);
        return this.page(page, query);
    }

    @Override
    public SysPrivilege SavePrivilege(SysPrivilege data) {
        Assert.hasText(data.getPrivilegeName(), "权限名称不能为空");

        if (!data.getType().equals("MENU")) {
            Assert.hasText(data.getPrivilegeCode(), "权限编码不能为空");
        }


        if (StrUtil.isEmptyIfStr(data.getPrivilegeId())) {
            data.setCreateBy(userUtil.GetUserName());
            data.setCreateTime(new Date());
            data.setUpdateBy(data.getCreateBy());
            data.setUpdateTime(new Date());
        } else {
            SysPrivilege oldPost = this.baseMapper.selectById(data.getPrivilegeId());
            if (oldPost != null) {
                data.setCreateTime(oldPost.getCreateTime());
                data.setCreateBy(oldPost.getCreateBy());
            }
            data.setUpdateBy(userUtil.GetUserName());
            data.setUpdateTime(new Date());

        }

        SysPrivilege oldPost = GetByCode(data.getPrivilegeCode());
        if (oldPost != null && !oldPost.getPrivilegeId().equals(data.getPrivilegeId())) {
            throw new BusinessException("权限编码已存在");
        }

        this.saveOrUpdate(data);
        return data;
    }

    private SysPrivilege GetByCode(String code) {

        QueryWrapper<SysPrivilege> query = new QueryWrapper<>();
        query.lambda().eq(SysPrivilege::getPrivilegeCode, code);
        return this.getOne(query, false);
    }

    @Override
    public Boolean DeletePrivilege(String privilegeId) {
        return this.removeById(privilegeId);
    }
}
